/**
 * 
 */
package com.cnhaimei.vod.logic;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.cnhaimei.com.PopupWindowEx;
import com.cnhaimei.vod.R;
import com.cnhaimei.vod.net.XVod;
import com.cnhaimei.vod.net.dc.Player;
import com.cnhaimei.vod.util.Const;

/**
 * 调音台
 * 
 * @author jin001.wu@gmail.com <br>
 * @date 2011-7-8
 * 
 */
public class MusicSetActivity extends VODActivity implements
		OnSeekBarChangeListener {
	TextView toneView;
	TextView micView;
	TextView musicView;
	
	View kView;
	View standView;
	View populeView;
	View barView;
	View cinemaView;
	View hallView;
	View partyView;
	View hostView;
	
	View lightSoft;
	View lightActive;
	View lightFace;
	View lightStrong;
	View switchOn;
	View switchOff;
	SeekBar toneBar;
	SeekBar audioBar;
	SeekBar micBar;
	private PopupWindowEx mPopupWindows;
	TextView popupTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.v8_music_set);

		initView();
		refreshView();
		XVod.G().registerListener(EventsManager.Event.PLYER_STATE, this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		XVod.G().unRegisterListener(EventsManager.Event.PLYER_STATE, this);
	}

	public void onEvent(int event, Object obj, boolean isSuc) {
		switch (event) {
		case EventsManager.Event.PLYER_STATE:
			refreshView();
			break;
		default:
			break;
		}
		super.onEvent(event, obj, isSuc);
	}

	@Override
	public void onClick(View v) {
		final Player p = Player.G();
		int id = v.getId();
		switch (id) {
		case R.id.goback:
			close();
			break;
		case R.id.effect_k:
			XVod.G().setPlayerSet(Player.PLAYER_SET_EFFECT, Const.EFFECT.K);
			kView.setSelected(true);		
			break;
		case R.id.popule:
			XVod.G().setPlayerSet(Player.PLAYER_SET_EFFECT, Const.EFFECT.POPULE);
			populeView.setSelected(true);			
			break;
		case R.id.stand:
			XVod.G().setPlayerSet(Player.PLAYER_SET_EFFECT, Const.EFFECT.STAND);
			standView.setSelected(true);		
			break;
		case R.id.bar:
			XVod.G().setPlayerSet(Player.PLAYER_SET_EFFECT, Const.EFFECT.BAR);
			barView.setSelected(true);		
			break;
		case R.id.cinema:
			XVod.G().setPlayerSet(Player.PLAYER_SET_EFFECT, Const.EFFECT.CINEMA);
			cinemaView.setSelected(true);		
			break;
		case R.id.hall:
			XVod.G().setPlayerSet(Player.PLAYER_SET_EFFECT, Const.EFFECT.HALL);
			hallView.setSelected(true);		
			break;
		case R.id.party:
			XVod.G().setPlayerSet(Player.PLAYER_SET_EFFECT, Const.EFFECT.PARTY);
			partyView.setSelected(true);		
			break;
		case R.id.host:
			XVod.G().setPlayerSet(Player.PLAYER_SET_EFFECT, Const.EFFECT.HOST);
			hostView.setSelected(true);		
			break;
			/*
		case R.id.light_active:
			XVod.G().setPlayerSet(Player.PLAYER_SET_LIGHT, Const.LIGHT.ACTIVE);
			break;
		case R.id.light_face:
			XVod.G().setPlayerSet(Player.PLAYER_SET_LIGHT, Const.LIGHT.FACE);
			break;
		case R.id.light_soft:
			XVod.G().setPlayerSet(Player.PLAYER_SET_LIGHT, Const.LIGHT.SOFT);
			break;
		case R.id.light_strong:
			XVod.G().setPlayerSet(Player.PLAYER_SET_LIGHT, Const.LIGHT.STRONG);
			break;
			*/
		case R.id.adjus_music: // ignore
			finish();
			break;
		case R.id.auto:
			break;
		case R.id.reset:
			XVod.G().setPlayerState(Player.PLAYER_SET_RESET);
			break;
		case R.id.tone_add: {
			int vol = p.getToneVolume();
			if (vol < p.getToneMax()) {
				XVod.G().setPlayerSet(Player.PLAYER_SET_TONE,
						vol + p.getToneScale()); // 请求增加
			}

		}
			break;
		case R.id.tone_reduse: {
			int vol = p.getToneVolume();
			if (vol > p.getToneMin()) {
				XVod.G().setPlayerSet(Player.PLAYER_SET_TONE,
						vol - p.getToneScale()); // 请求减少
			}
		}
			break;
		case R.id.music_add: {
			int vol = p.getAudioVolume();
			if (vol < p.getAudioMax()) {
				XVod.G().setPlayerSet(Player.PLAYER_SET_VOLUM,
						vol + p.getAudioScale()); // 请求增加
			}
		}
			break;
		case R.id.music_reduse: {
			int vol = p.getAudioVolume();
			if (vol > p.getAudioMin()) {
				XVod.G().setPlayerSet(Player.PLAYER_SET_VOLUM,
						vol - p.getAudioScale()); // 请求减少
			}
		}
			break;
		case R.id.mic_add: {
			int vol = p.getMicVolume();
			if (vol < p.getMicMax()) {
				XVod.G().setPlayerSet(Player.PLAYER_SET_MIC,
						vol + p.getMicScale()); // 请求增加
			}
		}
			break;
		case R.id.mic_reduse: {
			int vol = p.getMicVolume();
			if (vol > p.getMicMin()) {
				XVod.G().setPlayerSet(Player.PLAYER_SET_MIC,
						vol - p.getMicScale()); // 请求减少
			}
		}
			break;
		default:
			super.onClick(v); // need parent
			break;
		}

	}

	private void initView() {
		setTitle("调音");
		showBackBtn(true);
		
		setClickable(R.id.goback, R.id.tone_add, R.id.tone_reduse,
				R.id.mic_add, R.id.mic_reduse, R.id.music_add,
				R.id.music_reduse, R.id.reset, R.id.auto);
		setClickable(R.id.effect_k, R.id.popule, R.id.stand, R.id.cinema, R.id.bar, R.id.hall, R.id.party, R.id.host);
		setFocusable(R.id.effect_k, R.id.popule, R.id.stand, R.id.cinema, R.id.bar, R.id.hall, R.id.party, R.id.host);
		//setClickable(R.id.light_active, R.id.light_face, R.id.light_soft,	R.id.light_strong);
		
		setMusicTouchListener(R.id.tone_add, R.id.tone_reduse, R.id.mic_add,
				R.id.mic_reduse, R.id.music_add, R.id.music_reduse);
		
		toneView = (TextView) findViewById(R.id.tone_edit);
		micView = (TextView) findViewById(R.id.mic_edit);
		musicView = (TextView) findViewById(R.id.music_edit);

		kView = findViewById(R.id.effect_k);
		standView = findViewById(R.id.stand);
		populeView = findViewById(R.id.popule);
		barView = findViewById(R.id.bar);
		cinemaView = findViewById(R.id.cinema);
		hallView = findViewById(R.id.hall);
		partyView = findViewById(R.id.party);
		hostView = findViewById(R.id.host);

		lightSoft = findViewById(R.id.light_soft);
		lightActive = findViewById(R.id.light_active);
		lightFace = findViewById(R.id.light_face);
		lightStrong = findViewById(R.id.light_strong);
		switchOn = findViewById(R.id.switch_on);
		switchOff = findViewById(R.id.switch_off);

		toneBar = (SeekBar) findViewById(R.id.tone_seek);
		audioBar = (SeekBar) findViewById(R.id.music_seek);
		micBar = (SeekBar) findViewById(R.id.mic_seek);

		toneBar.setTag(Player.PLAYER_SET_TONE);
		audioBar.setTag(Player.PLAYER_SET_VOLUM);
		micBar.setTag(Player.PLAYER_SET_MIC);
		toneBar.setOnSeekBarChangeListener(this);
		audioBar.setOnSeekBarChangeListener(this);
		micBar.setOnSeekBarChangeListener(this);
		
		View v = LayoutInflater.from(this).inflate(R.layout.musice_set_tip,
				null);
		popupTextView = (TextView) v.findViewById(R.id.tip);
		popupTextView.setBackgroundResource(R.drawable.ico_up);
		mPopupWindows = new PopupWindowEx(popupTextView, 55, 47);

	}

	private void refreshView() {
		final Player p = Player.G();

		toneBar.setMax(p.getToneMax() - p.getToneMin());
		audioBar.setMax(p.getAudioMax() - p.getAudioMin());
		micBar.setMax(p.getMicMax() - p.getMicMin());

		toneView.setText(String.valueOf(p.getToneVolume()));
		micView.setText(String.valueOf(p.getMicVolume()));
		musicView.setText(String.valueOf(p.getAudioVolume()));
/*
		if (p.getLightState() == Const.LIGHT.ACTIVE) {
			lightActive.setBackgroundResource(R.drawable.light_active_active);
		} else {
			lightActive.setBackgroundResource(R.drawable.light_active);
		}

		if (p.getLightState() == Const.LIGHT.FACE) {
			lightFace.setBackgroundResource(R.drawable.light_face_active);
		} else {
			lightFace.setBackgroundResource(R.drawable.light_face);
		}

		if (p.getLightState() == Const.LIGHT.SOFT) {
			lightSoft.setBackgroundResource(R.drawable.light_soft_active);
		} else {
			lightSoft.setBackgroundResource(R.drawable.light_soft);
		}

		if (p.getLightState() == Const.LIGHT.STRONG) {
			lightStrong.setBackgroundResource(R.drawable.light_strong_active);
		} else {
			lightStrong.setBackgroundResource(R.drawable.light_strong);
		}
*/
		if (p.getEffectState() == Const.EFFECT.K) {
			kView.setBackgroundResource(R.drawable.v8_tone_right_pressed);
			kView.setSelected(true);
		} else {
			kView.setBackgroundResource(R.drawable.v8_tone_right_normal);
			kView.setSelected(false);
		}

		if (p.getEffectState() == Const.EFFECT.BAR) {
			barView.setBackgroundResource(R.drawable.v8_tone_mid_pressed);
			barView.setSelected(true);
		} else {
			barView.setBackgroundResource(R.drawable.v8_tone_mid_normal);
			barView.setSelected(false);
		}

		if (p.getEffectState() == Const.EFFECT.POPULE) {
			populeView.setBackgroundResource(R.drawable.v8_tone_mid_pressed);
			populeView.setSelected(true);
		} else {
			populeView.setBackgroundResource(R.drawable.v8_tone_mid_normal);
			populeView.setSelected(false);
		}

		if (p.getEffectState() == Const.EFFECT.STAND) {
			standView.setBackgroundResource(R.drawable.v8_tone_left_pressed);
			standView.setSelected(true);
		} else {
			standView.setBackgroundResource(R.drawable.v8_tone_left_normal);
			standView.setSelected(false);
		}

		if (p.getEffectState() == Const.EFFECT.CINEMA) {
			cinemaView.setSelected(true);
			cinemaView.setBackgroundResource(R.drawable.v8_tone_left_pressed);
		} else {
			cinemaView.setSelected(false);
			cinemaView.setBackgroundResource(R.drawable.v8_tone_left_normal);
		}

		if (p.getEffectState() == Const.EFFECT.HALL) {
			hallView.setSelected(true);
			hallView.setBackgroundResource(R.drawable.v8_tone_right_pressed);
		} else {
			hallView.setSelected(false);
			hallView.setBackgroundResource(R.drawable.v8_tone_right_normal);
		}

		if (p.getEffectState() == Const.EFFECT.PARTY) {
			partyView.setSelected(true);
			partyView.setBackgroundResource(R.drawable.v8_tone_left_pressed);
		} else {
			partyView.setSelected(false);
			partyView.setBackgroundResource(R.drawable.v8_tone_left_normal);
		}

		if (p.getEffectState() == Const.EFFECT.HOST) {
			hostView.setSelected(true);
			hostView.setBackgroundResource(R.drawable.v8_tone_right_pressed);
		} else {
			hostView.setSelected(false);
			hostView.setBackgroundResource(R.drawable.v8_tone_right_normal);
		}
/*
		if (p.getLightFollowEffect() == 1) {
			switchOn.setBackgroundResource(R.drawable.on_effect);
			switchOff.setBackgroundResource(R.drawable.off_normal);
		} else {
			switchOn.setBackgroundResource(R.drawable.on_normal);
			switchOff.setBackgroundResource(R.drawable.off_effect);
		}
*/
		toneBar.setProgress(p.getToneVolume() - p.getToneMin());
		audioBar.setProgress(p.getAudioVolume() - p.getAudioMin());
		micBar.setProgress(p.getMicVolume() - p.getMicMin());
	}

	private void close() {
		finish();
	}

	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		if (!fromUser) {
			return;
		}
		Object tag = seekBar.getTag();
		if (tag instanceof Integer) {
			int com = (Integer) tag;
			switch (com) {
			case Player.PLAYER_SET_VOLUM:
				musicView.setText(String.valueOf(progress));
				break;
			case Player.PLAYER_SET_TONE:
				toneView.setText(String.valueOf(progress));
				break;
			case Player.PLAYER_SET_MIC:
				micView.setText(String.valueOf(progress));
				break;
			default:
				break;
			}
		}
	}

	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		int progress = seekBar.getProgress();
		Object tag = seekBar.getTag();
		final Player p = Player.G();
		if (tag instanceof Integer) {
			int com = (Integer) tag;
			switch (com) {
			case Player.PLAYER_SET_VOLUM:
				XVod.G().setPlayerSet(Player.PLAYER_SET_VOLUM,
						progress + p.getAudioMin());
				break;
			case Player.PLAYER_SET_TONE:
				XVod.G().setPlayerSet(Player.PLAYER_SET_TONE,
						progress + p.getToneMin());
				break;
			case Player.PLAYER_SET_MIC:
				XVod.G().setPlayerSet(Player.PLAYER_SET_MIC,
						progress + p.getMicMin());
				break;
			default:
				break;
			}
		}
	}
	
	protected void setMusicTouchListener(int... ids) {
		for (int i = 0; i < ids.length; i++) {
			View v = findViewById(ids[i]);
			if (v != null)
				v.setOnTouchListener(mMusicTouchClick);
		}
	}

	private OnTouchListener mMusicTouchClick = new OnTouchListener() {

		public boolean onTouch(View v, MotionEvent event) {
			int id = v.getId();
			int resId = 0;
			int offx = - 10;
			int offy = - 84;
			final Player p = Player.G();
			switch (id) {
			case R.id.tone_add:
				resId = Math.min(p.getToneMax(),p.getToneVolume() + p.getToneScale());
				break;
			case R.id.tone_reduse:
				resId = Math.max(p.getToneMin(),p.getToneVolume() - p.getToneScale());
				break;
			case R.id.mic_add:
				resId = Math.min(p.getMicMax(),p.getMicVolume() + p.getMicScale());
				break;
			case R.id.mic_reduse:
				resId = Math.max(p.getMicMin(),p.getMicVolume() - p.getMicScale());
				break;
			case R.id.music_add: 
				resId = Math.min(p.getAudioMax(),p.getAudioVolume() + p.getAudioScale());
				break;
			case R.id.music_reduse: 
				resId = Math.max(p.getAudioMin(),p.getAudioVolume() - p.getAudioScale());
				break;
			default:
				break;
			}
			
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				showBtnTip(v, resId, offx, offy);
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_OUTSIDE:
				mPopupWindows.dismiss(true);
				break;

			default:
				break;
			}
			return false;
		}
	};
	
	private void showBtnTip(View arch, int tip, int offx, int offy) {
		if (mPopupWindows.isShowing()) {
			mPopupWindows.dismiss();
		}

		popupTextView.setText(String.valueOf(tip));
		mPopupWindows.showAsDropDown(arch, offx, offy);
	}

}
